import { Toast } from "antd-mobile";
import { reqcartdelete, reqcartedit, reqcartlist } from "../../http/api";

import { getuserInfo } from "./login";
let isRequest = true;
//state
const initState = {
  //列表
  list: [],
  allChecked: false,
};

//action type
const TYPES = {
  SHOP_CHANGE_LIST: "SHOP_CHANGE_LIST",
  SHOP_CHANGE_ALL_CHECKED: "SHOP_CHANGE_ALL_CHECKED",
};

//reducer
const reducer = (state = initState, action) => {
  switch (action.type) {
    //修改list
    case TYPES.SHOP_CHANGE_LIST:
      return {
        ...state,
        list: [...action.list],
      };
    case TYPES.SHOP_CHANGE_ALL_CHECKED:
      return {
        ...state,
        allChecked: action.allChecked,
      };
    default:
      return state;
  }
};

//action creator
export const actions = {
  //修改list action
  changeList: (list) => ({ type: TYPES.SHOP_CHANGE_LIST, list }),
  reqList: () => (dispatch, getState) => {
    let userInfo = JSON.parse(localStorage.getItem("isLogin")).uid;

    reqcartlist({ uid: userInfo }).then((res) => {
      let arr = res.data.list[1].content ? res.data.list[1].content : [];
      //旧的list
      let list = getList(getState());
      //补checked
      arr.forEach((item) => {
        let obj = list.find((i) => i.id == item.id);
        item.checked = obj ? obj.checked : false;
      });
      //修改list
      dispatch(actions.changeList(arr));
      isRequest = true;
      //修改全选
      dispatch(
        actions.changeAllChecked(
          arr.length > 0 ? arr.every((item) => item.checked) : false
        )
      );
    });
  },
  //数量加+
  add: (id) => (dispatch, getState) => {
    reqcartedit({
      id,
      type: 2,
    }).then((res) => {
      if (res.data.code == 200) {
        Toast.show({
          content: res.data.msg,
        });
        dispatch(actions.reqList());
      }
    });
  },
  //数量-
  sub: (id, num) => (dispatch, getState) => {
    if (num <= 1) {
      Toast.show({
        content: "宝贝不能再少了",
      });
      return;
    }
    if (isRequest) {
      isRequest = false;
      reqcartedit({
        id,
        type: 1,
      }).then((res) => {
        Toast.show({
          content: res.data.msg,
        });
      });
      dispatch(actions.reqList());
    }
  },
  //删除
  del: (id) => (dispatch, getState) => {
    reqcartdelete({ id }).then((res) => {
      if (res.data.code == 200) {
        Toast.show({
          content: res.data.msg,
        });
        dispatch(actions.reqList());
      }
    });
  },
  //修改全选
  changeAllChecked: (allChecked) => ({
    type: TYPES.SHOP_CHANGE_ALL_CHECKED,
    allChecked,
  }),
  //点了全选
  changeAll: () => (dispatch, getState) => {
    let allChecked = getAllChecked(getState()); //原来的全选
    let newBool = !allChecked;
    //修改全选
    dispatch(actions.changeAllChecked(newBool));
    //修改list
    let list = getList(getState());
    list.forEach((item) => (item.checked = newBool));
    dispatch(actions.changeList(list));
  },
  changeOne: (index) => (dispatch, getState) => {
    //修改list
    let list = getList(getState());
    list[index].checked = !list[index].checked;
    dispatch(actions.changeList(list));
    dispatch(actions.changeAllChecked(list.every((item) => item.checked)));
  },
  //修改全选
};

//selector导出数据给组件
export const getList = (state) => state.shop.list;
export const getAllChecked = (state) => state.shop.allChecked;
export const getAllprice = (state) => {
  let list = getList(state);
  return list.reduce((v, i) => (i.checked ? v + i.price * i.num : v), 0);
};
//导出reducer
export default reducer;

//添加购物车的问题 全选的问题
